/***   学生成绩管理系统   ***/
#include "../include/student_score_system.h" // 包含自定义头文件
#include <iostream>               // 定义基本输入输出流
#include <fstream>                // 该头文件定义了一个用于处理文本输出的ofstream类
#include <cstdlib>                // 定义函数exit()的原型

// 主函数
int main()
{
    showMenu();
    selectOption();
    return 0;
}

// 显示主菜单
void showMenu()
{
    // 使用名称空间
    using std::cout;
    using std::endl;
    cout << "=========================================================" << endl;
    cout << "*               欢迎使用学生成绩管理系统                *" << endl;
    cout << "=========================================================" << endl;
    cout << "=========================================================" << endl;
    cout << "*\t\t    功能选择菜单  \t\t\t*" << endl;
    cout << "=========================================================" << endl;
    cout << "*\t\t   [1] 录入学生信息     \t\t*" << endl;
    cout << "*\t\t   [2] 显示学生信息     \t\t*" << endl;
    cout << "*\t\t   [3] 保存学生信息     \t\t*" << endl;
    cout << "*\t\t   [4] 统计学生人数     \t\t*" << endl;
    cout << "*\t\t   [5] 查询学生信息     \t\t*" << endl;
    cout << "*\t\t   [6] 查看成绩排名     \t\t*" << endl;
    cout << "*\t\t   [7] 修改学生信息     \t\t*" << endl;
    cout << "*\t\t   [8] 删除学生信息     \t\t*" << endl;
    cout << "*\t\t   [9] 退出系统         \t\t*" << endl;
    cout << "=========================================================" << endl;
    cout << endl;
}

// 输入选项
void selectOption()
{
    // 使用名称空间
    using std::cin;
    using std::cout;
    using std::endl;
    cout << "请输入你的选项 (1-9): ";
    int n;
    double m;
    cin >> m;
    n = m;

    // 判断输入是否正确
    while (cin.fail() || n < 1 || n > 9 || n != m)
    {
        cin.clear();              // 重置错误输入标记
        while (cin.get() != '\n') // 删除错误输入
            continue;
        cout << "输入错误，请重新输入: ";
        cin >> m;
        n = m;
    }

    switch (n)
    {
    case 1:
        inputStudent(&sys);
        break;
    case 2:
        showStudent(&sys);
        break;
    case 3:
        saveInformation(&sys);
        break;
    case 4:
        countStudent(&sys);
        break;
    case 5:
        findStudent(&sys);
        break;
    case 6:
        sortScore(&sys);
        break;
    case 7:
        modifyStudent(&sys);
        break;
    case 8:
        deleteStudent(&sys);
        break;
    case 9:
        exitSystem();
        break;
    }
}

// 录入学生信息
void inputStudent(System *sys)
{
    // 使用名称空间
    using std::cin;
    using std::cout;
    using std::endl;
    if (sys->num == 1000) // 判断是否已达录入上限
    {
        cout << "系统已达录入上限，请重新输入: " << endl;
        system("pause");
        system("cls");
        showMenu();
        selectOption();
    }
    else
    {
        system("cls");
        cout << "===================================================" << endl;
        cout << "*                   录入学生成绩                  *" << endl;
        cout << "===================================================" << endl;

        // 姓名
        cout << "请输入学生姓名: ";
        cin >> sys->stu[sys->num].name_;

        // 学号
        cout << "请输入学生学号: ";
        cin >> sys->stu[sys->num].id_;

        // 性别
        cout << "请输入学生性别(1---男)/(2---女): ";
        double m;
        cin >> m;
        sys->stu[sys->num].sex_ = m;
        while (cin.fail() || (sys->stu[sys->num].sex_ != 1 && sys->stu[sys->num].sex_ != 2) || sys->stu[sys->num].sex_ != m) // 判断输入数字是否正确
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> m;
            sys->stu[sys->num].sex_ = m;
        }

        // 语文成绩
        cout << "请输入语文成绩: ";
        cin >> sys->stu[sys->num].language_;
        while (cin.fail() || (sys->stu[sys->num].language_ > 100 || sys->stu[sys->num].language_ < 0)) // 判断输入数字是否正确
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> sys->stu[sys->num].language_;
        }

        // 数学成绩
        cout << "请输入数学成绩: ";
        cin >> sys->stu[sys->num].math_;
        while (cin.fail() || (sys->stu[sys->num].math_ > 100 || sys->stu[sys->num].math_ < 0)) // 判断输入数字是否正确
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> sys->stu[sys->num].math_;
        }

        // 英语成绩
        cout << "请输入英语成绩: ";
        cin >> sys->stu[sys->num].english_;
        while (cin.fail() || (sys->stu[sys->num].english_ > 100 || sys->stu[sys->num].english_ < 0)) // 判断输入数字是否正确
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> sys->stu[sys->num].english_;
        }

        /*
         * 判断是否存在重复录入的情况
         * 考虑到可能存在同名，但学号不可能一样
         * 故只判断学号是否相同
         */
        for (int i = 0; i < sys->num; i++)
        {
            if (sys->stu[i].id_ == sys->stu[sys->num].id_)
            {
                cout << "错误，该学生信息已存在！" << endl;
                system("pause");
                system("cls");
                showMenu();
                selectOption();
            }
        }

        sys->num++; // 更新录入学生个数

        cout << "成绩录入成功，是否继续录入? (Y/N)";
        cin.get(); // 清空输入缓冲区
        char ch;
        while (cin.get(ch))
        {
            if (ch == 'Y' || ch == 'y' || ch == 'N' || ch == 'n') // 判断输入是否正确
            {
                if (ch == 'Y' || ch == 'y')
                {
                    system("cls");
                    inputStudent(sys);
                }
                else if (ch == 'N' || ch == 'n')
                {
                    system("cls");
                    showMenu();
                    selectOption();
                }
                break;
            }
            else
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "输入错误，请重新输入(Y/N): ";
            }
        }
    }
}

// 显示学生信息
void showStudent(const System *sys)
{
    system("cls");

    // 使用名称空间
    using std::cout;
    using std::endl;
    std::string str1 = "男";
    std::string str2 = "女";
    cout << "========================================================================" << endl;
    cout << "*                              显示学生信息                            *" << endl;
    cout << "========================================================================" << endl;
    if (sys->num == 0) // 判断系统中是否已存在学生信息
    {
        cout << "当前系统尚未录入学生成绩，无法显示学生信息." << endl;
    }
    else
    {
        for (int i = 0; i < sys->num; i++)
        {
            cout << "姓名:" << sys->stu[i].name_ << " "
                 << " 性别:" << (sys->stu[i].sex_ == 1 ? str1 : str2) << " "
                 << " 学号:" << sys->stu[i].id_ << " "
                 << " 语文:" << sys->stu[i].language_ << " "
                 << " 数学:" << sys->stu[i].math_ << " "
                 << " 英语:" << sys->stu[i].english_
                 << endl;
        }
    }
    system("pause");
    system("cls");
    showMenu();
    selectOption();
}

// 保存学生信息
void saveInformation(const System *sys)
{
    // 使用名称空间
    using std::cout;
    using std::endl;
    if (sys->num == 0) // 判断系统中是否已存在学生信息
    {
        cout << "当前系统尚未录入学生成绩,没有数据可以保存." << endl;
    }
    else
    {
        std::string str1 = "男";
        std::string str2 = "女";
        std::ofstream outFile;                   // 声明ofstream对象
        outFile.open("data.txt", std::ios::out); // 使用方法open()打开文件
        if (!outFile.is_open())                  // 检查文件是否被成功打开
        {
            cout << "无法打开文件" << endl;
            cout << "程序终止." << endl;
            exit(EXIT_FAILURE); // 终止程序
        }
        outFile << "姓名\t"
                << "性别\t"
                << "学号\t"
                << "语文成绩\t"
                << "数学成绩\t"
                << "英语成绩\n";
        for (int i = 0; i < sys->num; i++)
        {
            outFile << sys->stu[i].name_ << '\t' << (sys->stu[i].sex_ == 1 ? str1 : str2) << '\t' << sys->stu[i].id_ << '\t'
                    << sys->stu[i].language_ << '\t' << sys->stu[i].math_ << '\t' << sys->stu[i].english_ << endl;
        }
        cout << "数据保存成功！" << endl;
        outFile.close(); // 关闭文件
    }
    system("pause");
    system("cls");
    showMenu();
    selectOption();
}

// 统计学生数量
void countStudent(const System *sys)
{
    system("cls");
    using std::cout;
    using std::endl;
    cout << "===================================================" << endl;
    cout << "*                   统计学生个数                  *" << endl;
    cout << "===================================================" << endl;
    int man_num = 0;
    int woman_num = 0;
    for (int i = 0; i < sys->num; i++)
    {
        if (sys->stu[i].sex_ == 1)
            man_num++;
        else
            woman_num++;
    }
    int all_num = man_num + woman_num;
    cout << "当前系统中录入学生数量如下: " << endl;
    cout << "男生: " << man_num << "人" << endl;
    cout << "女生: " << woman_num << "人" << endl;
    cout << "总人数: " << all_num << "人" << endl;
    system("pause");
    system("cls");
    showMenu();
    selectOption();
}

// 查询学生信息
void findStudent(const System *sys)
{
    system("cls");

    // 使用名称空间
    using std::cin;
    using std::cout;
    using std::endl;
    cout << "===================================================" << endl;
    cout << "*                   查询学生信息                  *" << endl;
    cout << "===================================================" << endl;
    if (sys->num == 0) // 判断系统中是否已存在学生信息
    {
        cout << "当前系统尚未录入学生成绩，无法查询学生信息." << endl;
        system("pause");
        system("cls");
        showMenu();
        selectOption();
    }
    else
    {
        std::string str1 = "男";
        std::string str2 = "女";
        cout << "  [1] 按姓名查询" << endl;
        cout << "  [2] 按学号查询" << endl;
        cout << "===================================================" << endl;
        cout << "请输入选项以选择查询方式: ";
        int n; // 定义二级菜单输入选项
        double m;
        cin >> m;
        n = m;
        while (cin.fail() || n < 1 || n > 2 || n != m)
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> m;
            n = m;
        }

        switch (n)
        {
        case 1:
        {
            std::string name;
            cout << "请输入学生姓名: ";
            cin >> name;
            for (int i = 0; i < sys->num; i++)
            {
                if (name == sys->stu[i].name_)
                {
                    cout << "姓名: " << sys->stu[i].name_ << endl;
                    cout << "性别: " << (sys->stu[i].sex_ == 1 ? str1 : str2) << endl;
                    cout << "学号: " << sys->stu[i].id_ << endl;
                    cout << "语文: " << sys->stu[i].language_ << endl;
                    cout << "数学: " << sys->stu[i].math_ << endl;
                    cout << "英语: " << sys->stu[i].english_ << endl;

                    cout << "查询成功！" << endl;
                    cout << "是否继续查询？(Y/N)";
                    cin.get();
                    char dh;
                    while (cin.get(dh))
                    {
                        if (dh == 'Y' || dh == 'y' || dh == 'N' || dh == 'n') // 判断输入是否正确
                        {
                            if (dh == 'Y' || dh == 'y')
                            {
                                system("cls");
                                findStudent(sys);
                            }
                            else if (dh == 'N' || dh == 'n')
                            {
                                system("cls");
                                showMenu();
                                selectOption();
                            }
                            break;
                        }
                        else
                        {
                            cin.clear();
                            while (cin.get() != '\n')
                                continue;
                            cout << "输入错误，请重新输入(Y/N): ";
                        }
                    }
                    break;
                }
                else
                {
                    cout << "抱歉，没有找到姓名为" << name << "的学生" << endl;
                    system("pause");
                    system("cls");
                    showMenu();
                    selectOption();
                }
            }
        }
        break;
        case 2:
        {
            std::string id;
            cout << "请输入学生学号: ";
            cin >> id;
            for (int i = 0; i < sys->num; i++)
            {
                if (id == sys->stu[i].id_)
                {
                    cout << "姓名: " << sys->stu[i].name_ << endl;
                    cout << "性别: " << (sys->stu[i].sex_ == 1 ? str1 : str2) << endl;
                    cout << "学号: " << sys->stu[i].id_ << endl;
                    cout << "语文: " << sys->stu[i].language_ << endl;
                    cout << "数学: " << sys->stu[i].math_ << endl;
                    cout << "英语: " << sys->stu[i].english_ << endl;

                    cout << "查询成功！" << endl;
                    cout << "是否继续查询？(Y/N)";
                    cin.get(); // 清空输入缓冲区
                    char dh;
                    while (cin.get(dh))
                    {
                        if (dh == 'Y' || dh == 'y' || dh == 'N' || dh == 'n') // 判断输入是否正确
                        {
                            if (dh == 'Y' || dh == 'y')
                            {
                                system("cls");
                                findStudent(sys);
                            }
                            else if (dh == 'N' || dh == 'n')
                            {
                                system("cls");
                                showMenu();
                                selectOption();
                            }
                            break;
                        }
                        else
                        {
                            cin.clear();
                            while (cin.get() != '\n')
                                continue;
                            cout << "输入错误，请重新输入(Y/N): ";
                        }
                    }
                    break;
                }
                else
                {
                    cout << "抱歉，没有找到学号为" << id << "的学生" << endl;
                    system("pause");
                    system("cls");
                    showMenu();
                    selectOption();
                }
            }
        }
        break;
        }
    }
}

// 查看成绩排名 *
void sortScore(const System *sys)
{
    system("cls");

    // 使用名称空间
    using std::cin;
    using std::cout;
    using std::endl;
    cout << "===================================================" << endl;
    cout << "*                   查看成绩排名                  *" << endl;
    cout << "===================================================" << endl;
    if (sys->num == 0) // 判断系统中是否已存在学生信息
    {
        cout << "当前系统尚未录入学生成绩，无法查看成绩排名." << endl;
    }
    else
    {
        System *p_sys = new System; // 使用动态内存分配创建临时结构指针
        *p_sys = *sys;
        cout << "  [1] 语文成绩" << endl;
        cout << "  [2] 数学成绩" << endl;
        cout << "  [3] 英语成绩" << endl;
        cout << "  [4] 总成绩" << endl;
        cout << "===================================================" << endl;
        cout << "请输入选项以选择排名方式: ";
        int n; // 定义二级菜单输入项
        double m;
        cin >> m;
        n = m;
        while (cin.fail() || n < 1 || n > 4 || n != m) // 判断输入是否正确
        {
            cin.clear();              // 重置错误输入标记
            while (cin.get() != '\n') // 删除错误输入
                continue;
            cout << "输入错误，请重新输入: ";
            cin >> m;
            n = m;
        }

        switch (n)
        {
        case 1:
        {
            system("cls");
            cout << "===================================================" << endl;
            cout << "*                   查看成绩排名                  *" << endl;
            cout << "===================================================" << endl;
            double t = 0;

            // 冒泡排序
            for (int i = 0; i <= p_sys->num - 1; i++) // 先排最后一个数
            {
                for (int j = 0; j <= p_sys->num - i; j++) // 确定i个数的位置后，只需再排p_sys->num - i个数
                {
                    if (p_sys->stu[j].language_ < p_sys->stu[j + 1].language_) // 相邻两个数进行比较
                    {
                        t = p_sys->stu[j].language_;
                        p_sys->stu[j].language_ = p_sys->stu[j + 1].language_;
                        p_sys->stu[j + 1].language_ = t;
                    }
                }
            }
            cout << "名 次\t"
                 << "姓名\t"
                 << "分数\n";
            for (int k = 0; k < p_sys->num; k++)
            {
                cout << "第" << k + 1 << "名\t" << p_sys->stu[k].name_ << '\t' << p_sys->stu[k].language_ << endl;
            }
            delete p_sys; // 释放内存
        }
        break;
        case 2:
        {
            system("cls");
            cout << "===================================================" << endl;
            cout << "*                   查看成绩排名                  *" << endl;
            cout << "===================================================" << endl;
            double t = 0;

            // 冒泡排序
            for (int i = 0; i <= p_sys->num - 1; i++)
            {
                for (int j = 0; j <= p_sys->num - i; j++)
                {
                    if (p_sys->stu[j].math_ < p_sys->stu[j + 1].math_)
                    {
                        t = p_sys->stu[j].math_;
                        p_sys->stu[j].math_ = p_sys->stu[j + 1].math_;
                        p_sys->stu[j + 1].math_ = t;
                    }
                }
            }
            cout << "名 次\t"
                 << "姓名\t"
                 << "分数\n";
            for (int k = 0; k < p_sys->num; k++)
            {
                cout << "第" << k + 1 << "名\t" << p_sys->stu[k].name_ << '\t' << p_sys->stu[k].math_ << endl;
            }
            delete p_sys; // 释放内存
        }
        break;
        case 3:
        {
            system("cls");
            cout << "===================================================" << endl;
            cout << "*                   查看成绩排名                  *" << endl;
            cout << "===================================================" << endl;
            double t = 0;

            // 冒泡排序
            for (int i = 0; i <= p_sys->num - 1; i++)
            {
                for (int j = 0; j <= p_sys->num - i; j++)
                {
                    if (p_sys->stu[j].english_ < p_sys->stu[j + 1].english_)
                    {
                        t = p_sys->stu[j].english_;
                        p_sys->stu[j].english_ = p_sys->stu[j + 1].english_;
                        p_sys->stu[j + 1].english_ = t;
                    }
                }
            }
            cout << "名 次\t"
                 << "姓名\t"
                 << "分数\n";
            for (int k = 0; k < p_sys->num; k++)
            {
                cout << "第" << k + 1 << "名\t" << p_sys->stu[k].name_ << '\t' << p_sys->stu[k].math_ << endl;
            }
            delete p_sys; // 释放内存
        }
        break;
        case 4:
        {
            system("cls");
            cout << "===================================================" << endl;
            cout << "*                   查看成绩排名                  *" << endl;
            cout << "===================================================" << endl;
            double t = 0;
            double *all_score = new double[p_sys->num]; // 使用动态内存分配创建临时数组指针
            for (int i = 0; i < p_sys->num; i++)
            {
                // 遍历累加
                all_score[i] = p_sys->stu[i].language_ + p_sys->stu[i].math_ + p_sys->stu[i].english_;
            }

            // 冒泡排序
            for (int i = 0; i <= p_sys->num - 1; i++)
            {
                for (int j = 0; j <= p_sys->num - i; j++)
                {
                    if (all_score[j] < all_score[j + 1])
                    {
                        t = all_score[j];
                        all_score[j] = all_score[j + 1];
                        all_score[j + 1] = t;
                    }
                }
            }
            cout << "名 次\t"
                 << "姓名\t"
                 << "分数\n";
            for (int k = 0; k < p_sys->num; k++)
            {
                cout << "第" << k + 1 << "名\t" << p_sys->stu[k].name_ << '\t' << all_score[k] << endl;
            }
            delete[] all_score; // 释放内存
            delete p_sys;       // 释放内存
        }
        break;
        }
    }
    system("pause");
    system("cls");
    showMenu();
    selectOption();
}

// 修改学生信息
void modifyStudent(System *sys)
{
    system("cls");

    // 使用名称空间
    using std::cin;
    using std::cout;
    using std::endl;
    cout << "===================================================" << endl;
    cout << "*                   修改学生信息                  *" << endl;
    cout << "===================================================" << endl;
    if (sys->num == 0) // 判断系统中是否已存在学生信息
    {
        cout << "当前系统尚未录入学生信息，不存在可以修改的对象." << endl;
        system("pause");
        system("cls");
        showMenu();
        selectOption();
    }
    else
    {
        std::string id;
        std::string str1 = "男";
        std::string str2 = "女";
        cout << "请输入学生学号以查找修改对象: ";
        cin >> id;
        for (int i = 0; i < sys->num; i++)
        {
            if (id == sys->stu[i].id_)
            {
                cout << "姓名: " << sys->stu[i].name_ << endl;
                cout << "性别: " << (sys->stu[i].sex_ == 1 ? str1 : str2) << endl;
                cout << "学号: " << sys->stu[i].id_ << endl;
                cout << "语文: " << sys->stu[i].language_ << endl;
                cout << "数学: " << sys->stu[i].math_ << endl;
                cout << "英语: " << sys->stu[i].english_ << endl;

                cout << "确定要修改该学生的信息吗？(Y/N)";
                char dh;
                while (cin.get(dh))
                {
                    if (dh == 'Y' || dh == 'y' || dh == 'N' || dh == 'n') // 判断输入是否正确
                    {
                        if (dh == 'Y' || dh == 'y')
                        {
                            cout << "请重新输入该学生信息: " << endl;
                            cout << "姓名: ";
                            cin >> sys->stu[i].name_;

                            cout << "性别(1---男/2---女): ";
                            double m;
                            cin >> m;
                            sys->stu[i].sex_ = m;
                            while (cin.fail() || (sys->stu[i].sex_ != 1 && sys->stu[i].sex_ != 2) || sys->stu[i].sex_ != m) // 判断输入是否正确
                            {
                                cin.clear();              // 重置错误输入标记
                                while (cin.get() != '\n') // 删除错误输入
                                    continue;
                                cout << "输入错误，请重新输入: ";
                                cin >> m;
                                sys->stu[i].sex_ = m;
                            }

                            cout << "学号: ";
                            cin >> sys->stu[i].id_;

                            cout << "语文: ";
                            cin >> sys->stu[i].language_;
                            while (cin.fail() || (sys->stu[i].language_ > 100 || sys->stu[i].language_ < 0)) // 判断输入是否正确
                            {
                                cin.clear();              // 重置错误输入标记
                                while (cin.get() != '\n') // 删除错误输入
                                    continue;
                                cout << "输入错误，请重新输入: ";
                                cin >> sys->stu[i].language_;
                            }

                            cout << "数学: ";
                            cin >> sys->stu[i].math_;
                            while (cin.fail() || (sys->stu[i].math_ > 100 || sys->stu[i].math_ < 0)) // 判断输入是否正确
                            {
                                cin.clear();              // 重置错误输入标记
                                while (cin.get() != '\n') // 删除错误输入
                                    continue;
                                cout << "输入错误，请重新输入: ";
                                cin >> sys->stu[i].math_;
                            }

                            cout << "英语: ";
                            cin >> sys->stu[i].english_;
                            while (cin.fail() || (sys->stu[i].english_ > 100 || sys->stu[i].english_ < 0)) // 判断输入数字是否正确
                            {
                                cin.clear();              // 重置错误输入标记
                                while (cin.get() != '\n') // 删除错误输入
                                    continue;
                                cout << "输入错误，请重新输入: ";
                                cin >> sys->stu[i].english_;
                            }

                            cout << "信息修改成功！" << endl;
                            system("pause");
                            system("cls");
                            showMenu();
                            selectOption();
                        }
                        else if (dh == 'N' || dh == 'n')
                        {
                            system("cls");
                            showMenu();
                            selectOption();
                        }
                    }
                    else
                    {
                        cin.clear();
                        while (cin.get() != '\n')
                            continue;
                        cout << "输入错误，请重新输入(Y/N): ";
                    }
                }
            }
            else
            {
                cout << "抱歉，没有找到学号为" << id << "的学生" << endl;
                system("pause");
                system("cls");
                showMenu();
                selectOption();
            }
        }
    }
}

// 删除学生信息
void deleteStudent(System *sys)
{
    // 清屏
    system("cls");

    // 使用名称空间
    using std::cin;
    using std::cout;
    using std::endl;
    cout << "===================================================" << endl;
    cout << "*                   删除学生信息                  *" << endl;
    cout << "===================================================" << endl;
    if (sys->num == 0) // 判断系统中是否已存在学生信息
    {
        cout << "当前系统尚未录入学生信息，不存在可以删除的对象." << endl;
        system("pause");
        system("cls");
        showMenu();
        selectOption();
    }
    else
    {
        std::string id;
        std::string str1 = "男";
        std::string str2 = "女";
        cout << "请输入学生学号以查找删除对象: ";
        cin >> id;
        for (int i = 0; i < sys->num; i++)
        {
            if (id == sys->stu[i].id_)
            {
                cout << "姓名: " << sys->stu[i].name_ << endl;
                cout << "性别: " << (sys->stu[i].sex_ == 1 ? str1 : str2) << endl;
                cout << "学号: " << sys->stu[i].id_ << endl;
                cout << "语文: " << sys->stu[i].language_ << endl;
                cout << "数学: " << sys->stu[i].math_ << endl;
                cout << "英语: " << sys->stu[i].english_ << endl;

                cout << "确定要删除该学生的信息吗？(Y/N)";
                cin.get(); // 清空输入缓冲区
                char dh;
                while (cin.get(dh))
                {
                    if (dh == 'Y' || dh == 'y' || dh == 'N' || dh == 'n') // 判断输入是否正确
                    {
                        if (dh == 'Y' || dh == 'y')
                        {
                            for (int j = i; j < sys->num; j++)
                            {
                                sys->stu[j] = sys->stu[j + 1]; // 覆盖删除对象的信息
                            }
                            sys->num--; // 删除成功后，记录学生个数
                            cout << "删除成功." << endl;
                            system("pause");
                            system("cls");
                            showMenu();
                            selectOption();
                        }
                        else if (dh == 'N' || dh == 'n')
                        {
                            system("cls");
                            showMenu();
                            selectOption();
                        }
                        break;
                    }
                    else
                    {
                        cin.clear();              // 重置错误输入状态
                        while (cin.get() != '\n') // 清除错误输入
                            continue;
                        cout << "输入错误，请重新输入(Y/N): ";
                    }
                }
                break;
            }
            else
            {
                cout << "抱歉，没有找到学号为" << id << "的学生" << endl;
                system("pause");
                system("cls");
                showMenu();
                selectOption();
            }
        }
    }
}

// 退出系统
void exitSystem()
{
    std::cout << "程序终止.\n";
    /*
     * 函数exit()的原型是在头文件stdlib中定义的
     * 在该头文件中，还定义了一个用于同操作系统通信的参数值EXIT_FAILURE
     * 函数exit()终止程序
     */
    exit(EXIT_FAILURE);
}